/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assisisw.smartcal.notificationsubsystem;

import assisisw.smartcal.entitysubsystem.Event;
import assisisw.smartcal.entitysubsystem.IEventController;
import assisisw.smartcal.entitysubsystem.Notification;
import assisisw.smartcal.weathersubsystem.IWeatherCache;
import assisisw.smartcal.weathersubsystem.WeatherData;
import java.util.Calendar;
import java.util.Date;
import javax.ejb.EJB;
import javax.ejb.Schedule;

/**
 *
 * @author Francesco
 */
public class ThreeDayBeforeReminderBean 
{

    /***/
    @EJB ()
    private IEventController eventController ;
    
    /***/
    @EJB ()
    private IWeatherCache weatherCache ;
    
    /***/
    @EJB ()
    private IEntityControllerObserver entityControllerObserver ;
    
    /***/
    @Schedule ( dayOfMonth = "*" , persistent = true )
    public void check () 
    {
        Iterable < Event > candidates ;
        WeatherData weatherData ;
        Calendar c ;
        Date targetDate ;
        targetDate = new Date ();
        c = Calendar.getInstance();
        c.setTimeInMillis ( targetDate.getTime () );
        c.add ( Calendar.DAY_OF_MONTH , 3 ) ;
        targetDate = c.getTime () ;
        candidates = eventController.findByDate(targetDate);
        for ( Event e : candidates )
            if ( e.isOutdoor() )
                if ( e.getPlace () != null && e.getPlace ().trim().compareToIgnoreCase ( "" ) != 0 )
                {
                    weatherData = weatherCache.getEntry ( e.getPlace() , new Date () , 12 ) ;
                    if ( weatherData != null && isBadWeather ( weatherData )  )
                    {
                        int i ;
                        Calendar cal = Calendar.getInstance();
                        for ( i = 1 ; i <= 5 ; i ++ )
                        {
                            cal.add ( Calendar.DAY_OF_MONTH , 1 );
                            weatherData = weatherCache.getEntry ( e.getPlace() , cal.getTime() , 12 ) ;
                            if ( weatherData != null && ! isBadWeather(weatherData) )
                            {
                                entityControllerObserver.addNotification(Notification.NotificationType.WEATHER, e,e.getOwningCalendar().getOwner(), 
                                "About your event + "+e.getName()+" may be weather will be bad. " + weatherData.getIniDate().toLocaleString() + " should be better !" );
                            }
                        }
                        if ( i == 6 )
                        {
                                entityControllerObserver.addNotification(Notification.NotificationType.WEATHER, e,e.getOwningCalendar().getOwner(), "About your event + "+e.getName()+" may be weather will be bad. ");                            
                        }
                    }
                }
    }
    
    /***/
    private boolean isBadWeather ( WeatherData weatherData ) 
    {
        return weatherData.getDescription().toUpperCase().contains ( "RAIN" ) 
                || weatherData.getDescription().toUpperCase().contains("SNOW") 
                || weatherData.getTemeperature()<0 ;
    } 
    
}
